﻿using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入关键词：");
            ReadText(Console.ReadLine());
        }
        public static void ReadText(string input)
        {
            StreamReader sr;
            try
            {
                sr = File.OpenText("E:\\人工智能\\人工智能\\文本.txt");
                //key是第几行，value[0]为关键字在字符串的下标，value[1]为:以关键字串的下标为开始截取字符串至结束
                var fileContents = new Dictionary<int, List<string>>();
                int line = 0;
                while (!sr.EndOfStream)
                {
                    string value = sr.ReadLine();
                    fileContents.Add(++line, new List<string> { value.IndexOf(input).ToString(),
                        value.Substring(value.IndexOf(input))});
                }
                sr.Close();
                foreach (var item in fileContents.Keys)
                {
                    Console.WriteLine($"第{item}行,第{int.Parse(fileContents[item][0]) + 1}个字符开始:{fileContents[item][1].Substring(0, 10)}...");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
